package com.example.eclipseandroidcomponents;


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.ToggleButton;

public class FirstScreenSlidePageFragment extends Fragment{

	
	//component
	private Button button_hello;
	private Button button_hi;
	private AlertDialog dialog_exit;
	private AlertDialog dialog_hi;
	private SeekBar seekbar;
	private ProgressBar progressbar;
	private EditText edittext;
	private ToggleButton togglebutton_onoff;
	private CheckBox checkbox_active;

	
	//listener
	DialogInterface.OnClickListener dialoginterface;
	
	//component builder
	AlertDialog.Builder dialog_builder;
	
	//main context 
	Context main_context;
	
	//main toast for all game
	Toast main_toast;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.first_screen_slide_page, container, false);
        return rootView;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState){
		super.onActivityCreated(savedInstanceState);
		Initialize();		
	}
	
	private void Initialize(){
		main_context = getActivity();		
		
		//add listener use "variable" listener
		dialoginterface = new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				if(which == DialogInterface.BUTTON_POSITIVE)
					getActivity().finish();
				
			}
		};		
		dialog_builder = new AlertDialog.Builder(main_context);
		dialog_builder.setCancelable(true);
		dialog_builder.setMessage(R.string.alert_exit_title_text);
		dialog_builder.setPositiveButton(R.string.alert_exit_confirm_button_text, dialoginterface);
		dialog_builder.setNegativeButton(R.string.alert_exit_cancel_button_text, dialoginterface);
		dialog_exit = dialog_builder.create();
		
		//add listener use "variable" listener
		dialoginterface = new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
								
			}
		};
		
		dialog_builder = new AlertDialog.Builder(main_context);
		dialog_builder.setCancelable(false);
		dialog_builder.setTitle("Small Button");
		dialog_builder.setMessage(Html.fromHtml("<font color='#000000'>"+ getString(R.string.alert_hi_title_text_front) + "</font><font color='#669900'>" + getString(R.string.button_hi_text) + "</font><font color='#000000'>" + getString(R.string.alert_hi_title_text_back) + "</font>"));		
		dialog_builder.setPositiveButton(R.string.alert_hi_button_confirm_text, dialoginterface);
		dialog_hi = dialog_builder.create();
		
		button_hello = (Button) getActivity().findViewById(R.id.button_hello);
		//add listener use "new" OnClickListener
		button_hello.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ShowExitDialog();				
			}
		});
		
		button_hi = (Button) getActivity().findViewById(R.id.button_hi);
		button_hi.setText(R.string.button_hi_text);
		button_hi.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ShowHiDialog();				
			}
		});
		
		
		
		progressbar = (ProgressBar) getActivity().findViewById(R.id.progressBar2);		
		edittext = (EditText) getActivity().findViewById(R.id.editText_sample);
		seekbar = (SeekBar) getActivity().findViewById(R.id.seekBar1);
		seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				SetProgressValue(progress);
			}
		});			
		
		togglebutton_onoff = (ToggleButton) getActivity().findViewById(R.id.toggleButton1);
		togglebutton_onoff.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				main_toast = new Toast(main_context);
				main_toast.setView(getView());		
				main_toast = Toast.makeText(main_context, isChecked?"On":"Off", Toast.LENGTH_SHORT);
				main_toast.show();
			}
		});
		checkbox_active = (CheckBox) getActivity().findViewById(R.id.checkBox1);
		checkbox_active.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub	
				
				main_toast = new Toast(main_context);
				main_toast.setView(getView());				
				main_toast = Toast.makeText(main_context, isChecked?"Active":"Passive", Toast.LENGTH_LONG);
				/*
				if(isChecked)					
					main_toast = Toast.makeText(main_context, Html.fromHtml("<font color='#ffff00'> Active </font>"), Toast.LENGTH_SHORT);
				else
					main_toast = Toast.makeText(main_context, Html.fromHtml("<font color='#ff00ff'> Passive </font>"), Toast.LENGTH_SHORT);
				*/
				main_toast.show();
			}
		});
	}
	
	private void ShowExitDialog(){
		try{
		dialog_exit.show();
		}catch(Exception e){
			System.out.println(e);
			e.printStackTrace();
		}
	}
	
	private void ShowHiDialog(){
		dialog_hi.show();
	}
	
	private void SetProgressValue(int newvalue){
		progressbar.setProgress(newvalue);
		edittext.setText(Integer.toString(newvalue));
	}
}
